﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DevelopTool.Bll.ManageMenuBlls;
using DevelopTool.Bll.OffLineBlls;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.OffLineModels;
using Sunny.UI;

namespace DevelopTool.Forms.OffLineForms
{
    /// <summary>
    /// 离线数据管理
    /// </summary>
    public partial class OffLineManageForm : BaseUIPage
    {
        private ManageMenuBll manageMenull;

        private OffLineBll offLineBll;

        public OffLineManageForm()
        {
            InitializeComponent();
        }


        private void OffLineManageForm_Load(object sender,EventArgs e)
        {
            manageMenull = new ManageMenuBll();
            offLineBll = new OffLineBll();

            this.manageControl.BaseBll = offLineBll;

            LoadManageControl();

            this.manageControl.IsCanCreateNode = false;
        }


        /// <summary>
        /// 加载管理页控件
        /// </summary>
        private void LoadManageControl()
        {
            List<ManageMenu> manageMenus = new List<ManageMenu>();
            LoadingHelper.ShowLoading("加载菜单数据",() =>
            {
                manageMenus = offLineBll.GetList().Cast<ManageMenu>().ToList();
            });

            this.manageControl.SetMenuData(manageMenus);
        }


        /// <summary>
        /// 菜单项添加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdded(object sender,MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;

            if(e.TreeNode.Name == "TopNode")
            {
                nav.SetNodeSymbol(e.TreeNode,61641);
            }
            else if(e.TreeNode.Name == "Folder")
            {
                nav.SetNodeSymbol(e.TreeNode,61564);
            }
            else if(e.TreeNode.Name == "Node")
            {
                nav.SetNodeSymbol(e.TreeNode,361788);
            }
        }


        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="text"></param>
        private void ManageControl_MenuQuery(object sender,string text)
        {
            Dictionary<string,List<ManageMenu>> dic = new Dictionary<string,List<ManageMenu>>();

            //关联到的节点名
            var nameList = manageMenull.GetIdListByName<OffLine>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的离线项名",nameList);

            var timeList = manageMenull.GetIdListByTime<OffLine>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的日期",timeList);

            this.manageControl.SetMenuQueryData(dic);
        }


        /// <summary>
        /// 菜单节点双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDoubleClick(object sender,MenuEventArgs e)
        {
            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                OffLine offLine = offLineBll.GetById(e.ManageMenu.Id);

                this.manageControl.ShowPageForm(new OffLineForm(offLine),offLine.NodeName,true,offLine.NodeName + offLine.Id);
            }
        }


        /// <summary>
        /// 菜单节点拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDrag(object sender,MenuEventArgs e)
        {
            OffLine offLine = manageMenull.GetExample<OffLine>(e.ManageMenu);

            manageMenull.SimpleSave(offLine);
        }



        /// <summary>
        /// 删除菜单节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDelete(object sender,MenuEventArgs e)
        {
            OffLine offLine = manageMenull.GetExample<OffLine>(e.ManageMenu);

            if(offLineBll.Delete(offLine.Id))
            {
                this.ShowSuccessNotifier($"【{offLine.NodeName}】已删除");
            }
            else
            {
                e.Cancel = true;
                this.ShowWarningNotifier("不能删除非空目录");
            }
        }


        /// <summary>
        /// 重新加载菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuNeedReLoad(object sender,EventArgs e)
        {
            LoadManageControl();
        }


        /// <summary>
        /// 菜单节点重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuRename(object sender,MenuEventArgs e)
        {
            OffLine offLine = manageMenull.GetExample<OffLine>(e.ManageMenu);

            manageMenull.SimpleSave(offLine);
        }


        /// <summary>
        /// 新建菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdd(object sender,MenuEventArgs e)
        {
            //只能新建目录，不能建节点
            if(e.ManageMenu.NodeType == MenuNodeType.Folder)
            {
                OffLine offLine = manageMenull.GetExample<OffLine>(e.ManageMenu);

                offLine = manageMenull.SimpleSave(offLine);

                e.ManageMenu = offLine;

                this.ShowSuccessNotifier("新建成功");
            }
            else
            {
                e.Cancel = true;
            }
        }

    }
}
